﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Silverlight.Windows;
using SuperLauncher.Helpers;
using SuperLauncher.Views;
using System.Runtime.InteropServices.Automation;
using Microsoft.Silverlight.Windows.Platform;

namespace SuperLauncher
{
    public partial class MainPage : UserControl
    {
        InstallNESLPage install = new InstallNESLPage();

        internal string ApplicationID = "{22F3948D-0129-499D-AEF3-6081478BF8E6}";
        internal string AcqusitionToken = "{6CB4EEE6-B505-4BBE-B505-FCD1D834DF83}";

        public MainPage()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!Installer.CheckNESLInstalled(2, 1))
            {
                install.btnInstallNESL.Click += new RoutedEventHandler(btnInstallNESL_Click);
                this.LayoutRoot.Children.Add(install);
                Application.Current.MainWindow.Width = 480;
                Application.Current.MainWindow.Height = 600;
            }
            else
            {
                if (SingleInstanceMonitor.Current.Acquire(ApplicationID, AcqusitionToken) == false)
                {
                    Application.Current.MainWindow.Close();
                }

                if (Application.Current.IsRunningOutOfBrowser && Application.Current.HasElevatedPermissions && JumpListHelper.RunJumplistTask())
                {
                    Application.Current.MainWindow.Close();
                }
                else
                {
                    this.LayoutRoot.Children.Add(new JumpListItemsTreeView());
                    Application.Current.MainWindow.Width = 480;
                    Application.Current.MainWindow.Height = 600;
                    this.PinToTaskbar();
                }
            }
        }

        void btnInstallNESL_Click(object sender, RoutedEventArgs e)
        {
            this.install.btnInstallNESL.Content = "正在安装 NESL 扩展库...";
            this.install.btnInstallNESL.IsEnabled = false;

            if (!install.NESLInstallAtstartup())
            {
                MessageBox.Show("安装失败");
            }
            else
            {
                this.LayoutRoot.Children.Add(new JumpListItemsTreeView());
                Application.Current.MainWindow.Width = 480;
                Application.Current.MainWindow.Height = 600;
                this.PinToTaskbar();
            }
        }

        /// <summary>
        /// 自动锁定到任务栏
        /// </summary>
        private void PinToTaskbar()
        {
            using (dynamic ShellApplication = AutomationFactory.CreateObject("Shell.Application"))
            {
                string desktopPath = string.Empty;

                using (dynamic wsShell = AutomationFactory.CreateObject("Wscript.shell"))
                {
                    desktopPath = wsShell.SpecialFolders("Desktop");
                }

                if (!string.IsNullOrWhiteSpace(desktopPath))
                {
                    dynamic directory = ShellApplication.NameSpace(desktopPath);
                    if (directory != null)
                    {
                        dynamic link = directory.ParseName("便捷启动器.lnk");

                        if (link != null)
                        {
                            dynamic verbs = link.Verbs();
                            for (int i = 0; i < verbs.Count(); i++)
                            {
                                dynamic verb = verbs.Item(i);
                                if (verb.Name.Replace(@"&", string.Empty).ToLower() == "pin to taskbar"
                                    || verb.Name == "锁定到任务栏(&K)")
                                {
                                    verb.DoIt();
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
